package com.zxiw.zxedu.recyclerview.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 */
@SuppressLint("NotifyDataSetChanged")
abstract class HelperRecyclerViewAdapter<T>(context: Context, vararg layoutId: Int) :
    BaseRecyclerViewAdapter<T>(context, *layoutId), DataHelper<T> {
    override fun createViewHolder(view: View, layoutId: Int): BaseRecyclerViewHolder {
        return HelperRecyclerViewHolder(view, layoutId)
    }

    override fun onBindData(viewHolder: BH, position: Int, item: T) {
        val helperViewHolder = viewHolder as HelperRecyclerViewHolder
        helperBindData(helperViewHolder, position, item)
        //1.赋值相关事件,例如点击长按等
        //2.也可用低二种方式，用BaseRecyclerViewAdapter类中的 adapter.setOnItemClickListener()
        setListener(helperViewHolder, position, item)
    }

    protected abstract fun helperBindData(
        viewHolder: HelperRecyclerViewHolder,
        position: Int,
        item: T
    )

    protected open fun setListener(viewHolder: HelperRecyclerViewHolder, position: Int, item: T) {
    }

    override fun isEnabled(position: Int): Boolean {
        return position >= 0 && position < mList.size
    }

    override fun addItemToHead(data: T) {
        add(0, data)
    }

    override fun addItemToLast(data: T): Boolean {
        val result = mList.add(data)
        notifyDataSetChanged()
        return result
    }

    override fun addItemsToHead(datas: List<T>): Boolean {
        return addAll(0, datas)
    }

    override fun addItemsToLast(datas: List<T>): Boolean {
        val result = mList.addAll(datas)
        notifyDataSetChanged()
        return result
    }

    /**
     * 设置了item动画必须用notifyItemRangeInserted刷新适配器，否则没有动画效果
     *
     * @param datas
     * @return
     */
    fun addAndNotifyItems(datas: List<T>): Boolean {
        if (datas.isEmpty()) return false
        val pos = mList.size
        val result = mList.addAll(datas)
        notifyItemRangeInserted(pos, datas.size)
        return result
    }

    override fun addAll(startPosition: Int, datas: List<T>): Boolean {
        if (datas.isEmpty()) return false
        val result = mList.addAll(startPosition, datas)
        notifyDataSetChanged()
        return result
    }

    override fun add(startPosition: Int, data: T) {
        mList.add(startPosition, data)
        notifyDataSetChanged()
    }

    override fun getData(index: Int): T? {
        return if (itemCount == 0) null else mList[index]
    }

    override fun alterObj(oldData: T, newData: T) {
        alterObj(mList.indexOf(oldData),newData)
    }
    override fun alterObj(index: Int, data: T) {
        mList[index] = data
        notifyDataSetChanged()
    }

    override fun remove(data: T): Boolean {
        val result = mList.remove(data)
        notifyDataSetChanged()
        return result
    }

    override fun removeToIndex(index: Int) {
        mList.removeAt(index)
        notifyDataSetChanged()
    }

    override fun replaceAll(datas: List<T>) {
        mList.clear()
        addAll(0,datas)
    }

    override fun setListAll(datas: List<T>): Boolean {
        mList.clear()
        var result = false
        if(datas.isNotEmpty()){
            result = mList.addAll(datas)
        }
        notifyDataSetChanged()
        return result
    }

    override fun clear() {
        mList.clear()
        notifyDataSetChanged()
    }

    override fun contains(data: T): Boolean {
        if(mList.isEmpty()) return false
        return mList.contains(data)
    }
}